//
//  HRComposeView.swift
//  microBlog
//
//  Created by HR_W on 16/5/28.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit

class HRComposeView: UIView {
    private lazy var buttons: [HRComposeButton] = [HRComposeButton]()
    
    weak var target: UIViewController?
    
    func show(targetVc target: UIViewController){
        self.target = target
        target.view.addSubview(self)
    }
    override init(frame: CGRect) {
        let  rect = UIScreen.mainScreen().bounds
        super.init(frame: rect)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        for (index,btn) in buttons.reverse().enumerate() {
           startAnimation(btn, index: index, offset: 350)
        }
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        
        for (index,btn) in buttons.enumerate() {
          
            startAnimation(btn, index: index, offset: -350)
        }
    }
    
    private func startAnimation(btn: HRComposeButton,index: Int,offset: CGFloat){
        UIView.animateWithDuration(0.8, delay: Double(index) * 0.02, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: { 
            btn.center = CGPoint(x: btn.center.x, y: btn.center.y + offset)
            }) { (_) in
                
                if offset > 0 {
                    
                self.removeFromSuperview()
                }
        }
        
    
    }
    
    private func setupUI() {
    
        backgroundColor = UIColor.redColor()
        addSubview(backImageView)
        
        addSubview(sloganImage)
        
        sloganImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(self.snp_top).offset(100)
        }
        
        addChildButtons()
    
    }
    
    private func addChildButtons() {
    
        let count = composeItems.count
        
        let w: CGFloat = 80
        let h: CGFloat = 120
        let marginCom: CGFloat = (ScreenWidth - 3 * w) / 4
        for i in 0..<count {
            let btn = HRComposeButton()
//            btn.setImage(UIImage(named: "tabbar_compose_more"), forState: .Normal)
//            btn.setTitle("更多", forState: .Normal)
            let item = composeItems[i]
            
            btn.composeItem = item
           // btn.frame = CGRect(x: 0, y: 0, width: w, height: h)
            let row = i / 3
            let col = i % 3
            
            let x = CGFloat(col) * (marginCom + w) + marginCom
            let y = CGFloat(row) * (h + marginCom) + ScreenHeight
            btn.frame = CGRect(x: x, y: y, width: w, height: h)
            
            btn.addTarget(self, action: #selector(btnDidClick(_:)), forControlEvents: .TouchUpInside)
            self.addSubview(btn)
            buttons.append(btn)
        }

    }
    @objc private func btnDidClick(btn: HRComposeButton){
    
        for button in buttons {
        
            var scale: CGFloat = 0
            
            if button == btn {
                scale = 2.0
            }else{
                scale = 0.5
            }
        UIView.animateWithDuration(0.25, animations: { 
            button.alpha = 0
            button.transform = CGAffineTransformMakeScale(scale, scale)
        })
        }
    guard let target = btn.composeItem?.target else{
        
            return
        }
        let type = NSClassFromString(target) as! UIViewController.Type
        
        let vc = type.init()
        let nav = UINavigationController(rootViewController: vc)
        self.target?.presentViewController(nav, animated: true, completion: { 
            self.removeFromSuperview()
        })
        
    }
    
    
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    
    private lazy var sloganImage: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
    private lazy var composeItems: [HRComposeItem] = {
        var array = [HRComposeItem]()
        
    let path = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)
    
        let itemArray = NSArray(contentsOfFile: path!) as? [[String: AnyObject]]
    
        for item in itemArray!{
        
           let composeItem = HRComposeItem(dict: item)
            array.append(composeItem)
        }
        return array
    }()
}
